package com.letoy.edu.service.Impl;

import com.letoy.edu.dao.CourseMapper;
import com.letoy.edu.dao.LessonInfoMapper;
import com.letoy.edu.dao.ScheduleMapper;
import com.letoy.edu.entity.CourseSchedule;
import com.letoy.edu.entity.LessonInfo;
import com.letoy.edu.entity.OnlineCourseInfo;
import com.letoy.edu.service.CourseService;
import com.letoy.edu.vo.LessonTimeInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class CourseServiceImpl implements CourseService {


//    CourseMapper courseMapper;

    @Autowired
    LessonInfoMapper lessonInfoMapper;

    @Autowired
    ScheduleMapper scheduleMapper;


    @Cacheable(value = "Course", key = "'course'")
    @Override
    public HashMap<String, Object> findAllCourse() {

        HashMap<String, Object> hashMap = new HashMap<>();

        List<LessonInfo> LessonInfoList = lessonInfoMapper.findAllLesson();
        if (LessonInfoList.isEmpty()) {
            hashMap.put("status", "1");
            hashMap.put("msg", "查询失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "查询成功");
        hashMap.put("data", LessonInfoList);
        return hashMap;
    }

    @Cacheable(value = "CourseIncludingTime", key = "'course'")
    @Override
    public HashMap<String, Object> findAllCourseIncludingTime() {

        HashMap<String, Object> hashMap = new HashMap<>();
        ArrayList<LessonTimeInfo> LessonTimeInfoList = new ArrayList<>();


        List<LessonInfo> allCourse = lessonInfoMapper.findAllLesson();

        for (LessonInfo lessonInfo : allCourse) {
            int startClass = (int) lessonInfo.getStartClass();
            int endClass = (int) lessonInfo.getEndClass();

            String startTime = scheduleMapper.findStartTime(startClass);
            String endTime = scheduleMapper.findEndTime(endClass);
            LessonTimeInfo lessonTimeInfo = new LessonTimeInfo(lessonInfo, startTime, endTime);
            LessonTimeInfoList.add(lessonTimeInfo);
        }
        if (LessonTimeInfoList.isEmpty()) {
            hashMap.put("status", "1");
            hashMap.put("msg", "查询失败");
            return hashMap;
        }
        hashMap.put("status", "0");
        hashMap.put("msg", "查询成功");
        hashMap.put("data", LessonTimeInfoList);
        return hashMap;
    }


}
